package com.briup.server.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.common.constraints.Add;
import com.briup.common.constraints.Update;
import com.briup.common.response.Result;
import com.briup.common.utils.BeanUtil;
import com.briup.server.domain.bean.Role;
import com.briup.server.domain.dto.RoleModifiedDTO;
import com.briup.server.domain.dto.ConditionQueryDTO;
import com.briup.server.domain.vo.RoleDetailVO;
import com.briup.server.domain.vo.RoleVO;
import com.briup.server.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.groups.Default;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Validated
@Tag(name = "角色管理模块")
@RestController
@RequestMapping("/roles")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @Operation(summary = "查询所有角色（作为授予角色时的列表）")
    @GetMapping("/list")
    public Result<List<RoleVO>> list() {
        List<Role> list = roleService.list();
        List<RoleVO> roleVOList = BeanUtil.convertList(list, RoleVO.class);
        return Result.success(roleVOList)
                .apply("查询成功");
    }

    @Operation(summary = "分页查询角色信息")
    @GetMapping("/page")
    public Result<Page<RoleDetailVO>> page(ConditionQueryDTO queryDTO) {
        Page<RoleDetailVO> page = roleService.pageQuery(queryDTO);
        return Result.success(page)
                .apply("查询成功");
    }

    @Operation(summary = "新增角色信息（包括给角色绑定菜单权限）")
    @PostMapping
    public Result<?> save(@RequestBody
                          @Validated({Add.class, Default.class})
                          RoleModifiedDTO modifiedDTO) {
        roleService.saveOrUpdateRole(modifiedDTO);
        return Result.success();
    }

    @Operation(summary = "修改角色信息（包括给角色绑定菜单权限）")
    @PutMapping
    public Result<?> update(@RequestBody
                            @Validated({Update.class, Default.class})
                            RoleModifiedDTO modifiedDTO) {
        roleService.saveOrUpdateRole(modifiedDTO);
        return Result.success();
    }

    @Operation(summary = "删除角色信息")
    @DeleteMapping
    public Result<?> deleteBatch(@RequestBody
                                 @NotEmpty(message = "待删除的角色列表不能为空") List<Long> ids) {
        roleService.removeRole(ids);
        return Result.success();
    }
}
